<template>
    <div>
      <h1>科室列表</h1>
      <div>
        科室:<input type="text" placeholder="科室名称或科室介绍" v-model="query.name"/>
        创建时间:<input type="date" v-model="query.createtime"/>
        <input type="button" value="查询" class="btn btn-primary" @click="showlist"/>
      </div>
      <table class="table table-bordered">
        <tbody>
            <tr>
                <td>ID</td>
                <td>科室名称</td>
                <td>科室人数</td>
                <td>科室介绍</td>
                <td>添加人</td>
                <td>创建时间</td>
                <td>操作</td>
            </tr>
            <tr v-for="item in list">
                <td>{{item.departmentId}}</td>
                <td>{{item.departmentName}}</td>
                <td>{{item.departmentNumber}}</td>
                <td>{{item.departmentDesc}}</td>
                <td>{{item.adminName}}</td>
                <td>{{moment(item.createTime).format("YYYY-MM-DD")}}</td>
                <td>
                  <input type="button" value="删除" class="btn btn-danger" @click="del(item.departmentId)"/>
                  <input type="button" value="修改" class="btn btn-info" @click="update(item.departmentId)"/>
                </td>
            </tr>
        </tbody>
      </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref,reactive,onMounted } from 'vue';
import moment from 'moment';
import { useRoute,useRouter } from 'vue-router';
//接收值
const route=useRoute();
//跳转页面
const router=useRouter();

let list:any=ref([])

let query:any=reactive
({
    name:'',
    createtime:''
})
onMounted(()=>{
  showlist();
})
//显示科室信息
const showlist=()=>
{
   axios
   ({
      url:'/api/Department/ShowDepartment',
      method:'get',
      params:query
   })
   .then(res=>{
    list.value=res.data;
   })
   .catch(err=>{

   })
}
//删除
const del=(id:any)=>
{
  let f=confirm("确认删除吗?")
  {
    if(f)
    {
      axios
      ({
          url:'/api/Department/DepartmentDelete',
          method:'delete',
          params:
          {
            id:id
          }
      })
      .then(res=>{
          console.log(res);
          if(res.data>0)
          {
              alert("删除成功");
              showlist();
              return;
          }
          else
          {
              alert("删除失败");
              return;
          }
          
      })
      .catch(err=>{
          console.log(err);
          
      })
    }
  }
}

const update=(id:any)=>
{
  router.push
  ({
    path:'/departmentUpdate',
    query:
    {
      id:id
    }
  })
}



</script>

<style scoped>

</style>